package com.n5x.model.user;

import com.n5x.common.util.CipherHashUtil;
import com.n5x.model.AbstractModel;
import org.apache.commons.lang3.StringUtils;

public class UserAccount extends AbstractModel {

    /**
     * 用户账号密码加密盐
     */
    public static final String LOGIN_PASS_SALT = "n5x";

    public enum AccountType {
        手机号码, 微信登陆, QQ, 新浪微博;

        public static AccountType getAccountType(Integer ordinal) {
            if (ordinal == null) return null;
            int size = AccountType.values().length;
            if (ordinal < 0 || ordinal > size - 1) return null;
            return AccountType.values()[ordinal];
        }
    }

    /**
     * 编制密码
     *
     * @param password 密码
     * @return
     */
    public static String encodePassword(String password) {
        return CipherHashUtil.initHashService(password, LOGIN_PASS_SALT).computeHash();
    }

    /**
     * 登陆名
     */
    private String loginName = "";

    /**
     * 登陆密码
     */
    private String password = "";

    /**
     * 账号类型
     */
    private String type = AccountType.微信登陆.name();

    /**
     * 绑定用户ID
     */
    private String fkUserId;

    public String getLoginName() {
        return loginName;
    }

    public UserAccount setLoginName(String loginName) {
        this.loginName = loginName;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public UserAccount setPassword(String password) {
        if (StringUtils.isNotBlank(password)) {
            password = encodePassword(password);
        }
        this.password = password;
        return this;
    }

    public String getType() {
        return type;
    }

    public UserAccount setType(String type) {
        this.type = type;
        return this;
    }

    public String getFkUserId() {
        return fkUserId;
    }

    public void setFkUserId(String fkUserId) {
        this.fkUserId = fkUserId;
    }
}